﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Configuration;
using ExtBusiness.User;
using ExtBusiness.Interface;

public partial class WebPubManager_UserEdit : System.Web.UI.Page
{
    private UserManager cUser = new UserManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                DataTable dt = cUser.GetUser(Request["id"]);

                if (dt.Rows.Count > 0)
                {
                    txtUserId.Text = dt.Rows[0]["USERID"].ToString();
                    txtUserName.Text = dt.Rows[0]["USERNAME"].ToString();
                    trUserPwd.Visible = false;
                    trUserRePwd.Visible = false;
                    ddlZjlx.Value = dt.Rows[0]["IDTYPE"].ToString();
                    txtZjhm.Text = dt.Rows[0]["IDNUMBER"].ToString();
                    txtTel.Text = dt.Rows[0]["TEL"].ToString();
                    txtCz.Text = dt.Rows[0]["FAX"].ToString();
                    txtMobile.Text = dt.Rows[0]["MOBILE"].ToString();
                    txtEmail.Text = dt.Rows[0]["EMAIL"].ToString();
                    txtYZBM.Text = dt.Rows[0]["POSTCODE"].ToString();
                    txtAddress.Text = dt.Rows[0]["ADDRESS"].ToString();
                    hfUsertype.Value = dt.Rows[0]["type"].ToString();//用户类型 addby zhongjian 20090904
                }

                //设置身份证类型的验证
                ddlZjlx.ValidatorName = CustomValidator1.ClientID;
                Page.ClientScript.RegisterStartupScript(this.GetType(), "knh1", "<script>ddlChange('" + ddlZjlx.ddlClientID + "');</script>");
            }
        }
    }

    protected void btnRefer_Click(object sender, EventArgs e)
    {
        string strId = Request["id"];
        string strCszcUserType = ConfigurationManager.AppSettings["csyhSystemFlag"];
        if (string.IsNullOrEmpty(strId))
        {
            if (HiddenFieldFlag.Value == "0")
            {
                return;
            }
            else if (HiddenFieldFlag.Value == "1")
            {
                return;
            }
            else if (HiddenFieldFlag.Value == "2")
            {
                return;
            }
            else if (HiddenFieldFlag.Value == "3")
            {
                return;
            }
            else
            {
                bool userExist = UserManager.CheckUserExist(txtUserId.Text);
                if (!userExist)
                {
                    UserManager.CreateUser(txtUserId.Text.ToUpper(), txtUserName.Text, txtUserPwd.Text,
                        int.Parse(strCszcUserType), txtTel.Text, txtMobile.Text,
                        txtAddress.Text, ddlZjlx.Value, txtZjhm.Text, txtCz.Text, txtEmail.Text,
                        txtYZBM.Text);                   

                    Response.Redirect("UserList.aspx");
                }
                else
                {
                    labMsg.Text = "用户名已存在，请重新输入！";
                }
            }
        }
        else
        {
            strCszcUserType = hfUsertype.Value;//在作修改时,应该保存原有的用户类型 addby zhongjian 20090904
            UserManager.UpdateUser(strId, txtUserId.Text, txtUserName.Text,
                int.Parse(strCszcUserType), txtTel.Text, txtMobile.Text,
                txtAddress.Text, ddlZjlx.Value, txtZjhm.Text, txtCz.Text, txtEmail.Text,
                txtYZBM.Text);

            //更新用户信息            
            InterfaceOperation infaceOperation = new InterfaceOperation();
            string strXml = infaceOperation.GetUserInfoXml(strId,"update");

            if (!string.IsNullOrEmpty(strXml))
            {
                try
                {

                    string strCheckCode = Common.Encode.Md5(DateTime.Now.ToShortDateString());

                    infaceOperation.UpdateExceptionAndXml(strXml, "接口返回值为空", 10);
                }
                catch (Exception ex)
                {
                    infaceOperation.UpdateExceptionAndXml(strXml, ex.Message, 10);
                }
            }
        }
    }

    #region 返回菜单管理主界面
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserList.aspx");
    }
    #endregion
}
